package com.jic.market.impl.rest.promotion.promotionInfoHandler;

import com.google.common.base.Joiner;
import com.google.common.collect.Lists;
import com.jic.common.base.vo.PageResult;
import com.jic.common.utils.DateUtils;
import com.jic.market.common.enums.DeleteFlagEnum;
import com.jic.market.common.enums.MutexFlagEnum;
import com.jic.market.common.enums.TypeOfProductEnum;
import com.jic.market.entity.*;
import com.jic.market.enums.FrontPromTypeConditionTypeEnum;
import com.jic.market.enums.FrontPromotionTypeEnum;
import com.jic.market.enums.PromotionProductTypeEnum;
import com.jic.market.enums.PromotionStatusEnum;
import com.jic.market.mapper.PromotionEntityMapper;
import com.jic.market.mapper.PromotionGiftItemEntityMapper;
import com.jic.market.mapper.PromotionMerchantProductEntityMapper;
import com.jic.market.mapper.PromotionRuleEntityMapper;
import com.jic.market.orm.Pagination;
import com.jic.market.request.rest.promotion.PromotionInfoRestRequest;
import com.jic.market.response.rest.promotion.PromotionDetailRestResponse;
import com.jic.market.response.rest.promotion.PromotionGiftItemRestResponse;
import com.jic.market.response.rest.promotion.PromotionGoodsSpuRestResponse;
import com.jic.market.response.rest.promotion.PromotionRuleRestResponse;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.Comparator;
import java.util.List;
import java.util.Set;
import java.util.stream.Collectors;


/**
 * description: PromotionComposeHandler
 * date: 2020/4/8 16:38
 * author: wangys
 * version: 1.0
 * 满额，单一，单品X,赠送，组合，换购, 预售（排除尾款结算）
 */
@Service
@Slf4j
public class PromotionComposeHandler implements BaseInfoHandler {
    @Autowired
    PromotionMerchantProductEntityMapper promotionMerchantProductEntityMapper;

    @Autowired
    PromotionEntityMapper promotionEntityMapper;

    @Autowired
    PromotionRuleEntityMapper promotionRuleEntityMapper;

    @Autowired
    PromotionGiftItemEntityMapper promotionGiftItemEntityMapper;

    final static List<Integer> EXCLUDE_FRONT_TYPE =
            Lists.newArrayList(FrontPromotionTypeEnum.SECKILL_SECKILL.getCode(),
                    FrontPromotionTypeEnum.SECKILL_SECKILL.getCode(),FrontPromotionTypeEnum.BITE_PRICE.getCode());
//    final static List<Integer> EXCLUDE_FRONT_TYPE = Lists.newArrayList();

    @Override
    public String handlerCode() {
        return "common_promotion_handler";
    }

    @Override
    public List<Integer> frontPromTypes() {
        return Lists.newArrayList(
                FrontPromotionTypeEnum.SINGLELY_SPECIAL,
                FrontPromotionTypeEnum.SINGLELY_DISCOUNT,
                FrontPromotionTypeEnum.SINGLELY_FALL,
                FrontPromotionTypeEnum.FULL_REDUCE,
                FrontPromotionTypeEnum.FULL_DISCOUNT,
                FrontPromotionTypeEnum.GIFT_FULL_QUOTA,
                FrontPromotionTypeEnum.GIFT_FULL_NUM,
                FrontPromotionTypeEnum.XOFFER_DI_X_Y_ZE,
                FrontPromotionTypeEnum.XOFFER_DI_X_Y_YUAN,
                FrontPromotionTypeEnum.XOFFER_MAN_X_Y_ZE,
//                FrontPromotionTypeEnum.INCREASE_FULL_QUOTA,
//                FrontPromotionTypeEnum.INCREASE_FULL_NUM,
//                FrontPromotionTypeEnum.BITE_PRICE,
//                FrontPromotionTypeEnum.INCREASE_BUY_GOODS,
                FrontPromotionTypeEnum.COMPLEX_X_YUAN_Y,
                FrontPromotionTypeEnum.COMPLEX_X_JIAN_FU_Y

//                FrontPromotionTypeEnum.FULL_NUM_REDUCE,
//                FrontPromotionTypeEnum.FULL_NUM_DISCOUNT,
//                FrontPromotionTypeEnum.FULL_NUM_REDUCE_LOGISTICS,

//                FrontPromotionTypeEnum.SECKILL_SECKILL
        ).stream().map(FrontPromotionTypeEnum::getCode).collect(Collectors.toList());
        //return Lists.newArrayList(1, 2, 3, 6, 7, 8, 9, 15, 16, 19, 12, 13, 14, 22, 23, 18);
    }

    @Override
    public boolean enable() {
        return true;
    }

    /**
     * 支持spu,sku
     * @param request
     * @return
     */
    @Override
    public List<PromotionDetailRestResponse> apply(PromotionInfoRestRequest request) {
        final List<PromotionDetailRestResponse> promotions = new ArrayList<>();
        PromotionMerchantProductEntity merchantProductEntity = new PromotionMerchantProductEntity();
        merchantProductEntity.setMpCode(request.getThirdProductCode());
        //merchantProductEntity.setTypeOfProduct(PromotionProductTypeEnum.TYPE_SPU.getType());
        merchantProductEntity.setDeleteFlag(DeleteFlagEnum.UN_DELETE.getCode());
        List<PromotionMerchantProductEntity> merchantProductEntities = promotionMerchantProductEntityMapper.findAll(merchantProductEntity);
        if (CollectionUtils.isEmpty(merchantProductEntities)) {
            return promotions;
        }
        //根据活动id查询出进行中的活动列表
        Set<Long> promotionIds = merchantProductEntities.stream().map(PromotionMerchantProductEntity::getPromotionId).collect(Collectors.toSet());
        PromotionEntity promotionEntity = new PromotionEntity();
        promotionEntity.setIds(promotionIds);
        promotionEntity.setStatus(PromotionStatusEnum.ING.getCode());
        promotionEntity.setDeleteFlag(DeleteFlagEnum.UN_DELETE.getCode());
        List<PromotionEntity> promotionEntities = promotionEntityMapper.findOfPage(promotionEntity, Pagination.build(1, Integer.MAX_VALUE));
        if (CollectionUtils.isEmpty(promotionEntities)) {
            log.info("getPromotinInfo promotions is empty mpId = {}", request.getThirdProductCode());
            return promotions;
        }


        //组装返回值(promotion-promotionRule-promotionGiftItem)
        promotionEntities.stream()
                .sorted(Comparator.comparing(PromotionEntity::getCreateTime).reversed())
                .forEach(pe -> {
                    promotions.add(genPromotionDetailRestResponse(pe));
        });

        return promotions;
    }

    private PromotionDetailRestResponse genPromotionDetailRestResponse(PromotionEntity pe) {
        List<PromotionRuleRestResponse> promotionRuleList = null;
        List<PromotionGiftItemRestResponse> giftItemRestResponses = null;
        PromotionDetailRestResponse restResponse = new PromotionDetailRestResponse()
                .setEndTime(pe.getEndTime().getTime())
                .setStartTime(pe.getStartTime().getTime())
                .setPromotionCode(pe.getPromotionCode())
                .setPromotionType(pe.getPromType())
                .setFrontPromotionType(pe.getFrontPromType())
                .setStartTimeStr(DateUtils.getFormatDateTime(pe.getStartTime()))
                .setPromName(pe.getPromName())
                .setPromId(pe.getId())
                .setEndTimeStr(DateUtils.getFormatDateTime(pe.getEndTime()));

        //查询出protionRule
        PromotionRuleEntity ruleEntity = new PromotionRuleEntity();
        ruleEntity.setPromotionId(pe.getId());
        ruleEntity.setDeleteFlag(DeleteFlagEnum.UN_DELETE.getCode());
        List<PromotionRuleEntity> promotionRuleEntities = promotionRuleEntityMapper.findAll(ruleEntity);
        if (CollectionUtils.isNotEmpty(promotionRuleEntities)) {
            promotionRuleList = promotionRuleEntities.stream().map(re -> {
                PromotionRuleRestResponse ruleRestResponse = new PromotionRuleRestResponse();
                BeanUtils.copyProperties(re, ruleRestResponse);
                return ruleRestResponse;
            }).collect(Collectors.toList());
        }

        //查询出protiongiftItem
        PromotionGiftItemEntity giftItemEntity = new PromotionGiftItemEntity();
        giftItemEntity.setPromotionId(pe.getId());
        giftItemEntity.setDeleteFlag(DeleteFlagEnum.UN_DELETE.getCode());
        List<PromotionGiftItemEntity> giftItemEntities = promotionGiftItemEntityMapper.findAll(giftItemEntity);
        if (CollectionUtils.isNotEmpty(giftItemEntities)) {
            giftItemRestResponses = giftItemEntities.stream().map(ge -> {
                PromotionGiftItemRestResponse giftItemRestResponse = new PromotionGiftItemRestResponse();
                BeanUtils.copyProperties(ge, giftItemRestResponse);
                return giftItemRestResponse;
            }).collect(Collectors.toList());
        }

        //根据frontType和rule生成description
        restResponse.setDescription(genDescription(pe, promotionRuleEntities));
        restResponse.setPromotionRuleList(promotionRuleList);
        restResponse.setPromotionGiftDetailList(giftItemRestResponses);
        return restResponse;
    }

    /**
     * 根据活动类型和规则生成描述
     *
     * @param promotionEntity
     * @param ruleEntities
     * @return
     */
    private String genDescription(PromotionEntity promotionEntity, List<PromotionRuleEntity> ruleEntities) {
        StringBuilder stringBuilder = new StringBuilder("");
        Integer frontPromType = promotionEntity.getFrontPromType();
        List<StringBuilder> builderList = null;
        FrontPromTypeConditionTypeEnum frontPromTypeConditionTypeEnum = FrontPromTypeConditionTypeEnum.lookByFrontType(promotionEntity.getFrontPromType());
        BigDecimal conditionMultiply = frontPromTypeConditionTypeEnum != null ? frontPromTypeConditionTypeEnum.getConditionMultiply() : new BigDecimal(1);
        BigDecimal contentValueMultiply = frontPromTypeConditionTypeEnum != null ? frontPromTypeConditionTypeEnum.getContentValueMultiply() : new BigDecimal(1);
        if (StringUtils.isNotBlank(promotionEntity.getDescription())) {
            return promotionEntity.getDescription();
        }
        switch (frontPromType) {
            case 1://单一&特价
            case 2://单一&折扣
            case 3://单一&直降
            case 17://预售&尾款结算
            case 18://预售&一口价
                //nothing
                break;
            case 6://满额&满额减
                if(promotionEntity.getOverlayAppFlag() == 1 && promotionEntity.getOverlayAppNum() != null && promotionEntity.getOverlayAppNum() > 0){
                    //每满x元减y元，可享受n次
                   builderList = ruleEntities.stream().map(r -> {
                        StringBuilder sb = new StringBuilder();
                        sb.append("每满").append(new BigDecimal(r.getConditionValue()).divide(conditionMultiply)).append("元减").append(r.getContentValue().divide(contentValueMultiply)).append("元").append("可享受").append(promotionEntity.getOverlayAppNum()).append("次");
                        return sb;
                    }).collect(Collectors.toList());
                }else{
                    //满x元减y元
                    builderList = ruleEntities.stream().map(r -> {
                        StringBuilder sb = new StringBuilder();
                        sb.append("满").append(new BigDecimal(r.getConditionValue()).divide(conditionMultiply)).append("元减").append(r.getContentValue().divide(contentValueMultiply)).append("元");
                        return sb;
                    }).collect(Collectors.toList());
                }
                stringBuilder = new StringBuilder(Joiner.on(";").join(builderList));
                break;
            case 7://满额&满额折
                if(promotionEntity.getOverlayAppFlag() == 1 && promotionEntity.getOverlayAppNum() != null && promotionEntity.getOverlayAppNum() > 0){
                    //每满x元打y折，可享受n次
                    builderList = ruleEntities.stream().map(r -> {
                        StringBuilder sb = new StringBuilder();
                        sb.append("每满").append(new BigDecimal(r.getConditionValue()).divide(conditionMultiply)).append("元打").append(r.getContentValue().divide(contentValueMultiply)).append("折").append("可享受").append(promotionEntity.getOverlayAppNum()).append("次");
                        return sb;
                    }).collect(Collectors.toList());
                }else{
                    //满x元y折
                    builderList = ruleEntities.stream().map(r -> {
                        StringBuilder sb = new StringBuilder();
                        sb.append("满").append(new BigDecimal(r.getConditionValue()).divide(conditionMultiply)).append("元打").append(r.getContentValue().divide(contentValueMultiply)).append("折");
                        return sb;
                    }).collect(Collectors.toList());
                }
                stringBuilder = new StringBuilder(Joiner.on(";").join(builderList));
                break;
            case 8://赠送&满金额赠
                if(promotionEntity.getOverlayAppFlag() == 1 && promotionEntity.getOverlayAppNum() != null && promotionEntity.getOverlayAppNum() > 0){
                    //每满x元送赠品，可享n次
                    builderList = ruleEntities.stream().map(r -> {
                        StringBuilder sb = new StringBuilder();
                        sb.append("每满").append(new BigDecimal(r.getConditionValue()).divide(conditionMultiply)).append("元送赠品，").append("可享").append(promotionEntity.getOverlayAppNum()).append("次");
                        return sb;
                    }).collect(Collectors.toList());
                }else{
                    //满x元送赠品，赠完为止
                    builderList = ruleEntities.stream().map(r -> {
                        StringBuilder sb = new StringBuilder();
                        sb.append("满").append(new BigDecimal(r.getConditionValue()).divide(conditionMultiply)).append("元送赠品，赠完为止");
                        return sb;
                    }).collect(Collectors.toList());
                }
                stringBuilder = new StringBuilder(Joiner.on(";").join(builderList));
                break;
            case 9://赠送&满商品赠
                if(promotionEntity.getOverlayAppFlag() == 1 && promotionEntity.getOverlayAppNum() != null && promotionEntity.getOverlayAppNum() > 0){
                    //每满x件送赠品，可享n次
                    builderList = ruleEntities.stream().map(r -> {
                        StringBuilder sb = new StringBuilder();
                        sb.append("每满").append(new BigDecimal(r.getConditionValue()).divide(conditionMultiply)).append("件送赠品，").append("可享").append(promotionEntity.getOverlayAppNum()).append("次");
                        return sb;
                    }).collect(Collectors.toList());
                }else{
                    //满x件送赠品，赠完为止
                    builderList = ruleEntities.stream().map(r -> {
                        StringBuilder sb = new StringBuilder();
                        sb.append("满").append(new BigDecimal(r.getConditionValue()).divide(conditionMultiply)).append("件送赠品，赠完为止");
                        return sb;
                    }).collect(Collectors.toList());
                }
                stringBuilder = new StringBuilder(Joiner.on(";").join(builderList));
                break;
            case 15://换购&满金额换购
                //满x元换购商品
                builderList = ruleEntities.stream().map(r -> {
                    StringBuilder sb = new StringBuilder();
                    sb.append("满").append(new BigDecimal(r.getConditionValue()).divide(conditionMultiply)).append("元换购商品");
                    return sb;
                }).collect(Collectors.toList());
                break;
            case 16://换购&买商品换购
                //满x件换购商品
                builderList = ruleEntities.stream().map(r -> {
                    StringBuilder sb = new StringBuilder();
                    sb.append("满").append(new BigDecimal(r.getConditionValue()).divide(conditionMultiply)).append("件换购商品");
                    return sb;
                }).collect(Collectors.toList());
                stringBuilder = new StringBuilder(Joiner.on(";").join(builderList));
                break;
            case 19://满额&满额减运费
                if(promotionEntity.getFreightType() == 1){
                    //1满x元，免运费，2
                    stringBuilder.append("满").append(promotionEntity.getFreightValue()).append("元，免运费");
                }else{
                    //满x元减y元运费
                    stringBuilder.append("满").append(promotionEntity.getFreightValue()).append("元减").append(promotionEntity.getFreightReducedValue()).append("元运费");
                }
                break;
            case 12://单品X&第X件Y折
                if(promotionEntity.getOverlayAppFlag() == 1 && promotionEntity.getOverlayAppNum() != null && promotionEntity.getOverlayAppNum() > 0){
                    //每第x件y折，可享受n次
                    builderList = ruleEntities.stream().map(r -> {
                        StringBuilder sb = new StringBuilder();
                        sb.append("每第").append(new BigDecimal(r.getConditionValue()).divide(conditionMultiply)).append("件").append(r.getContentValue().divide(contentValueMultiply)).append("折").append("可享受").append(promotionEntity.getOverlayAppNum()).append("次");
                        return sb;
                    }).collect(Collectors.toList());
                }else{
                    //第x件y折
                    builderList = ruleEntities.stream().map(r -> {
                        StringBuilder sb = new StringBuilder();
                        sb.append("第").append(new BigDecimal(r.getConditionValue()).divide(conditionMultiply)).append("件").append(r.getContentValue().divide(contentValueMultiply)).append("折");
                        return sb;
                    }).collect(Collectors.toList());
                }
                stringBuilder = new StringBuilder(Joiner.on(";").join(builderList));
                break;
            case 13://单品X&第X件Y元
                if(promotionEntity.getOverlayAppFlag() == 1 && promotionEntity.getOverlayAppNum() != null && promotionEntity.getOverlayAppNum() > 0){
                    //每第x件y元，可享受n次
                    builderList = ruleEntities.stream().map(r -> {
                        StringBuilder sb = new StringBuilder();
                        sb.append("每第").append(new BigDecimal(r.getConditionValue()).divide(conditionMultiply)).append("元").append(r.getContentValue().divide(contentValueMultiply)).append("折").append("可享受").append(promotionEntity.getOverlayAppNum()).append("次");
                        return sb;
                    }).collect(Collectors.toList());
                }else{
                    //第x件y元
                    builderList = ruleEntities.stream().map(r -> {
                        StringBuilder sb = new StringBuilder();
                        sb.append("第").append(new BigDecimal(r.getConditionValue()).divide(conditionMultiply)).append("元").append(r.getContentValue().divide(contentValueMultiply)).append("折");
                        return sb;
                    }).collect(Collectors.toList());
                }
                stringBuilder = new StringBuilder(Joiner.on(";").join(builderList));
                break;
            case 14://单品X&满X件Y折
                //满x件y折
                builderList = ruleEntities.stream().map(r -> {
                    StringBuilder sb = new StringBuilder();
                    sb.append("满").append(new BigDecimal(r.getConditionValue()).divide(conditionMultiply)).append("件").append(r.getContentValue().divide(contentValueMultiply)).append("折");
                    return sb;
                }).collect(Collectors.toList());
                stringBuilder = new StringBuilder(Joiner.on(";").join(builderList));
                break;
            case 22://组合X&x元y件
                //每满x件付y元
                builderList = ruleEntities.stream().map(r -> {
                    StringBuilder sb = new StringBuilder();
                    sb.append("满").append(new BigDecimal(r.getConditionValue()).divide(conditionMultiply)).append("件").append(r.getContentValue().divide(contentValueMultiply)).append("折");
                    return sb;
                }).collect(Collectors.toList());
                stringBuilder = new StringBuilder(Joiner.on(";").join(builderList));
                break;
            case 23://组合X&x件付y件
                builderList = ruleEntities.stream().map(r -> {
                    StringBuilder sb = new StringBuilder();
                    sb.append("满").append(new BigDecimal(r.getConditionValue()).divide(conditionMultiply)).append("件").append(r.getContentValue().divide(contentValueMultiply)).append("折");
                    return sb;
                }).collect(Collectors.toList());
                stringBuilder = new StringBuilder(Joiner.on(";").join(builderList));
                break;
            case 4:
                if(promotionEntity.getOverlayAppFlag() == 1 && promotionEntity.getOverlayAppNum() != null && promotionEntity.getOverlayAppNum() > 0){
                    builderList = ruleEntities.stream().map(r -> {
                        StringBuilder sb = new StringBuilder();
                        sb.append("每满").append(new BigDecimal(r.getConditionValue()).divide(conditionMultiply)).append("件减").append(r.getContentValue().divide(contentValueMultiply)).append("元").append("可享受").append(promotionEntity.getOverlayAppNum()).append("次");
                        return sb;
                    }).collect(Collectors.toList());
                }else{
                    builderList = ruleEntities.stream().map(r -> {
                        StringBuilder sb = new StringBuilder();
                        sb.append("满").append(new BigDecimal(r.getConditionValue()).divide(conditionMultiply)).append("件减").append(r.getContentValue().divide(contentValueMultiply)).append("元");
                        return sb;
                    }).collect(Collectors.toList());
                }
                stringBuilder = new StringBuilder(Joiner.on(";").join(builderList));
                break;
            case 5:
                if(promotionEntity.getOverlayAppFlag() == 1 && promotionEntity.getOverlayAppNum() != null && promotionEntity.getOverlayAppNum() > 0){
                    builderList = ruleEntities.stream().map(r -> {
                        StringBuilder sb = new StringBuilder();
                        sb.append("每满").append(new BigDecimal(r.getConditionValue()).divide(conditionMultiply)).append("件").append(r.getContentValue().divide(contentValueMultiply)).append("折").append("可享受").append(promotionEntity.getOverlayAppNum()).append("次");
                        return sb;
                    }).collect(Collectors.toList());
                }else{
                    builderList = ruleEntities.stream().map(r -> {
                        StringBuilder sb = new StringBuilder();
                        sb.append("满").append(new BigDecimal(r.getConditionValue()).divide(conditionMultiply)).append("件").append(r.getContentValue().divide(contentValueMultiply)).append("折");
                        return sb;
                    }).collect(Collectors.toList());
                }
                stringBuilder = new StringBuilder(Joiner.on(";").join(builderList));
                break;
            case 27:
                if(promotionEntity.getFreightType() == 1){
                    //1满x件，免运费，2
                    stringBuilder.append("满").append(promotionEntity.getFreightValue()).append("件，免运费");
                }else{
                    //满x件减y元运费
                    stringBuilder.append("满").append(promotionEntity.getFreightValue()).append("件减").append(promotionEntity.getFreightReducedValue()).append("元运费");
                }
                break;
        }
        return stringBuilder.toString();
    }

    @Override
    public PromotionDetailRestResponse promotionDetail(PromotionInfoRestRequest request) {
        PromotionEntity quereyParam =  new PromotionEntity();
        quereyParam.setPromotionCode(request.getPromotionCode());
        return genPromotionDetailRestResponse(promotionEntityMapper.query(quereyParam));
    }

    @Override
    public List<PromotionEntity> promotionBaseDetail(PromotionInfoRestRequest request) {
        List<PromotionEntity> promotionEntities = new ArrayList<>();
        PromotionMerchantProductEntity merchantProductEntity = new PromotionMerchantProductEntity();
//        merchantProductEntity.setMpCode(request.getThirdProductCode());
        //merchantProductEntity.setTypeOfProduct(PromotionProductTypeEnum.TYPE_SPU.getType());
        merchantProductEntity.setMpId(request.getMpId());
        merchantProductEntity.setDeleteFlag(DeleteFlagEnum.UN_DELETE.getCode());
        merchantProductEntity.setMutexFlag(MutexFlagEnum.UN_MUTEX.getCode());
        merchantProductEntity.setTypeOfProduct(request.getProductType());
        List<PromotionMerchantProductEntity> merchantProductEntities = promotionMerchantProductEntityMapper.findAll(merchantProductEntity);
        //如果是spu，是否互斥需要根据sku判断
        if(request.getProductType().equals(PromotionProductTypeEnum.TYPE_SPU.getType())){
            merchantProductEntities = merchantProductEntities.stream()
                    .filter(x->{
                        PromotionMerchantProductEntity skuParam = new PromotionMerchantProductEntity();
                        skuParam.setPromotionId(x.getPromotionId());
                        skuParam.setMpParentId(x.getMpId());
                        skuParam.setDeleteFlag(DeleteFlagEnum.UN_DELETE.getCode());
                        skuParam.setMutexFlag(MutexFlagEnum.UN_MUTEX.getCode());
                        return promotionMerchantProductEntityMapper.countOfPage(skuParam) > 0;
                    })
                    .collect(Collectors.toList());
        }

        if (CollectionUtils.isEmpty(merchantProductEntities)) {
            return promotionEntities;
        }
        Set<Long> promotionIds = merchantProductEntities.stream().map(PromotionMerchantProductEntity::getPromotionId).collect(Collectors.toSet());
        PromotionEntity promotionEntity = new PromotionEntity();
        promotionEntity.setIds(promotionIds);
        promotionEntity.setStatus(PromotionStatusEnum.ING.getCode());
        promotionEntity.setDeleteFlag(DeleteFlagEnum.UN_DELETE.getCode());
        promotionEntities = promotionEntityMapper.findOfPage(promotionEntity, Pagination.build(1, Integer.MAX_VALUE)).stream().filter(x -> {
            return !EXCLUDE_FRONT_TYPE.contains(x.getFrontPromType());
        }).collect(Collectors.toList());
        return promotionEntities;
    }

    @Override
    public PromotionEntity getPromotionDetailByPromotionCode(String promotionCode) {
        PromotionEntity param = new PromotionEntity();
        param.setPromotionCode(promotionCode);
        return promotionEntityMapper.query(param);
    }

    @Override
    public PageResult<PromotionGoodsSpuRestResponse> getGoodsByPromotionCode(PromotionInfoRestRequest request, Long promotionId) {
        List<PromotionGoodsSpuRestResponse> spuRestResponseList = new ArrayList<>();
        Pagination pagination = Pagination.build(request.getStart(), request.getLimit());
        PromotionMerchantProductEntity param = new PromotionMerchantProductEntity();
        param.setPromotionId(promotionId);
        param.setTypeOfProduct(request.getProductType() == null ? PromotionProductTypeEnum.TYPE_SPU.getType() : request.getProductType());
        param.setDeleteFlag(DeleteFlagEnum.UN_DELETE.getCode());
        param.setMutexFlag(MutexFlagEnum.UN_MUTEX.getCode());
        List<PromotionMerchantProductEntity> ofPage = promotionMerchantProductEntityMapper.findMutexOfPage(param, pagination);
        int count = promotionMerchantProductEntityMapper.countMutexOfPage(param);

        List<PromotionMerchantProductEntity> skuEntitys = null;
        //查询所有生效sku列表
        if(request.getProductType() == null){
            PromotionMerchantProductEntity skuParam = new PromotionMerchantProductEntity();
            skuParam.setPromotionId(promotionId);
            skuParam.setTypeOfProduct(PromotionProductTypeEnum.TYPE_SKU.getType());
            skuParam.setDeleteFlag(DeleteFlagEnum.UN_DELETE.getCode());
            skuParam.setMutexFlag(MutexFlagEnum.UN_MUTEX.getCode());
            skuEntitys = promotionMerchantProductEntityMapper.findAll(skuParam);
        }

        if(CollectionUtils.isNotEmpty(ofPage)){
            for (PromotionMerchantProductEntity productEntity : ofPage) {
                List<PromotionGoodsSpuRestResponse> skuResponses = null;
                //查询所有生效sku列表
                if(request.getProductType() == null && CollectionUtils.isNotEmpty(skuEntitys)){
                    skuResponses = skuEntitys.stream()
                            .filter(s->{return productEntity.getMpId() != null && s.getMpParentId().equals(productEntity.getMpId());})
                            .map(s->{
                                return new PromotionGoodsSpuRestResponse()
                                        .setThirdProductCode(s.getMpCode())
                                        .setGoodsName(s.getMpName());
                            }).collect(Collectors.toList());
                }

                spuRestResponseList.add(
                        new PromotionGoodsSpuRestResponse()
                        .setThirdProductCode(productEntity.getMpCode())
                        .setGoodsName(productEntity.getMpName())
                        .setChildrenProductList(skuResponses)
                );
            }
        }
        PageResult pageResult = new PageResult();
        pageResult.setRows(spuRestResponseList);
        pageResult.setTotal(count);
        return pageResult;
    }
}